require('./index.css')
require('page/common/top/index.js')

var _account_service = require('service/account-service.js');
const _util = require('util/util.js');

var userRegister = {
    
    init: function () {
        $('#registMsg').hide()
        this.bindEvents();
        
        return this;
    },
    bindEvents: function () {
        var _this = this
        $('#regist-btn').click(
            function(){
                _this.submit()
        })
    },
    submit : function(){
        var formData = {
            username : $.trim($('#username').val()),
            password : $.trim($('#password').val()),
            repeatedPassword : $.trim($('#repeatedPassword').val()),
            email :  $.trim($('#email').val()),
            city :  $.trim($('#city').val()),
            state :  $.trim($('#state').val()),
            zip :  $.trim($('#zip').val()),
            country :  $.trim($('#country').val()),
            firstName :  $.trim($('#firstName').val()),
            lastName :  $.trim($('#lastName').val()),
            address1 :  $.trim($('#address1').val()),
            status :  $.trim($('#status').val()),
            phone :  $.trim($('#phone').val()),
            language :  $.trim($('#language').val()),
            favcategory :  $.trim($('#favcategory').val()),
        };
        var validationResult = this.formValidation(formData);
        if(validationResult.status){
            _account_service.regist(
                formData,
                function(res){
                    window.location.href = _util.getURLParam('redirect') || './main.html';
                    // _util.getURLParam('redirect');
                },
                function(errMsg){
                    $('#registMsg').show().text(errMsg)
                }   
                );
            //向服务器发送请求
        }
        else{
            $('#registMsg').show().text(validationResult.msg)
        }
            
    },
    formValidation : function(formData){
        var result = {
            status : false,
            msg    : ''
        };
        if(!_util.validateField('require',formData.username)){
            result.msg = '用户名不能为空';
            return result;
        }
        if(!_util.validateField('require',formData.password)){
            result.msg = '密码不能为空';
            return result;
        }
        if(!_util.validateField('require',formData.email)){
            result.msg = '邮箱不能为空';
            return result;
        }
        if(!_util.validateField('require',formData.city)){
            result.msg = '城市不能为空';
            return result;
        }
        if(!_util.validateField('require',formData.zip)){
            result.msg = 'zip不能为空';
            return result;
        }
        if(!_util.validateField('require',formData.country)){
            result.msg = '省份不能为空';
            return result;
        }
        if(!_util.validateField('require',formData.phone)){
            result.msg = '电话不能为空';
            return result;
        }
        if(!_util.validateField('require',formData.firstName)){
            result.msg = '姓不能为空';
            return result;
        }
        if(!_util.validateField('require',formData.lastName)){
            result.msg = '名不能为空';
            return result;
        }
        if(!_util.validateField('require',formData.address1)){
            result.msg = 'addr1不能为空';
            return result;
        }
        if(!_util.validateField('require',formData.status)){
            result.msg = 'status不能为空';
            return result;
        }
        if(!_util.validateField('require',formData.language)){
            result.msg = 'language不能为空';
            return result;
        }
        if(!_util.validateField('require',formData.favcategory)){
            result.msg = 'favcategory不能为空';
            return result;
        }
        if(formData.password != formData.repeatedPassword){
            result.msg = '两个密码不一致';
            return result;
        }
        //校验通过
        result.status = true;
        result.msg = '校验通过';
        return result;
    }
};

module.exports = userRegister.init();